package org.brisling.satisfaction.queryService;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ChartDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.duties.domain.DutiesInquiry;
import org.brisling.satisfaction.domain.SatisRank;
import org.brisling.sysman.domain.Users;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/satisRank/query")
public class QuerySatisRankService extends AbstractBaseQueryService<SatisRank>{
	
	
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	@Autowired
	private QueryDictionaryService dicService;
	
	
	private String deptid_code = "litigant_deptid";
	
	HashMap<String,String> typeMap = null;
	public QuerySatisRankService(){
		this.moduleName = ConstSysParam.MODULE_SATISFACTION_RANK;
		this.subSystem = ConstSysParam.SUBSYSTEM_STATISFACTION;
		this.tableName = "V_" +this.moduleName;
		this.clazz = SatisRank.class;
		
		
//		orderMap.put("inquiry_date","desc");			
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("inquiry_date", "desc");
		orderMap.put("bizno", "desc");
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getInfoPage.action")
	public ModelAndView getInfoPage(){
		return new ModelAndView("/"+this.subSystem+"/"+this.moduleName+"/popInfo");
	}
	
		
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<SatisRank> getListDomain(@RequestBody(required=false) String body){		
		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getOperateTaskListDomain.action")
	public ListDomain<SatisRank> getUserCanoperateTask(
			@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_can_operate");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserTaskCanOperateListDomain(userid,sql, conditionMap);
	}
	
	@RequestMapping("/getSignedTaskListDomain.action")
	public ListDomain<SatisRank> getUserSignedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_signed");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserSignedListDomain(userid,sql, conditionMap,null);
	}
	
	@RequestMapping("/getUnsignedTaskListDomain.action")
	public ListDomain<SatisRank> getUserUnsignedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_can_signed");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserUnsignedListDomain(userid,sql, conditionMap,null);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getFinishedTaskListDomain.action")
	public ListDomain<SatisRank> getUserFinishedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_finished");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_satisfaction_all"))
			((Map<String,String>)conditionMap.get(ConstSysParam.CONDITION_MAP_KEY)).put(deptid_code,SysConfig.getUsr(userid).getDeptid().toString());
		return super.getUserFinishedListDomain(sql, conditionMap,null);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getUserManagedTaskDomain.action")
	public ListDomain<SatisRank> getUserManagedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_managed_manageuser");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_duties_all")){
			Users usr = SysConfig.getUsr(userid);
			if(this.isManageJob(userid)){
				conditionMap = this.addDeptConditionToMap(userid,conditionMap, deptid_code);
			}else{
				Map<String,String> paramMap = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
				if(paramMap==null){
					paramMap = new HashMap<String,String>();
				}
				paramMap.put("user_id_", usr.getIdentify());
				conditionMap.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);
				sql = dicService.getSqlByBizcode("flow_managed_normaluser"); 
			}
		}
		return super.getUserManagedListDomain(sql, conditionMap,null);
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/getTaskListDomain.action")
	public ListDomain<SatisRank> getTaskListDomain(
			@RequestBody(required=false) String body){
		
		
		ListDomain<SatisRank> domains = new ListDomain<SatisRank>();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if((conditionMap!=null) && (conditionMap.containsKey("condition"))){
			Map<String,String> cMap = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
			String _state = null;
			if((cMap!=null) && (cMap.containsKey("flow_state"))){
				_state = cMap.get("flow_state");				
			}else{
				_state = ConstSysParam.FLOW_CANOPERATE;
			}	
			
			
			switch(_state){
			case ConstSysParam.FLOW_CANOPERATE:
				domains = getUserCanoperateTask(body);
				break;
//			case ConstSysParam.FLOW_SIGNED:
//				domains = getUserSignedTask(body);
//				break;
//			case ConstSysParam.FLOW_UNSIGED:
//				domains = getUserUnsignedTask(body);
//				break;
			case ConstSysParam.FLOW_MANAGED:
				domains = getUserManagedTask(body);
				break;
			case ConstSysParam.FLOW_FINISHED:
				domains = getUserFinishedTask(body);
				break;
			default:
				domains = getUserCanoperateTask(body);
				break;
			}
		}
		return domains;
	}
	
	
	@RequestMapping("/getTreeListDomain.action")
	public ListDomain<SatisRank> getTreeListDomain(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeListDomain(id);
		
	}	
	
	@RequestMapping("/getTreeList.action")
	public List<SatisRank> getTreeList(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeList(id);
		
	}
	
	
	/**
	 * 获取任务数量
	 * @return
	 */
	@RequestMapping("/getTaskCnt.action")
	public List<ChartDomain> getTaskCnt(@RequestBody(required=false) String body){
		
		List<ChartDomain> domains = new ArrayList<ChartDomain>();
		String issue = "duties";
		Long canOpera = null;
		Long managed = null;
		Long finished = null;
		ListDomain<SatisRank> inquirys = this.getUserCanoperateTask(null);
		
		canOpera = inquirys.getTotal();
		ChartDomain operate = new ChartDomain("待处理",issue,canOpera);
		domains.add(operate);
		
		inquirys = this.getUserManagedTask(null);
		managed = inquirys.getTotal();
		ChartDomain manage = new ChartDomain("处理中",issue,managed);
		domains.add(manage);
		
		inquirys = this.getUserFinishedTask(body);
		finished = inquirys.getTotal();
		ChartDomain finish = new ChartDomain("已处理",issue,finished);
		domains.add(finish);
		
		return domains;
		
	}
	
	@RequestMapping("/getOne.action")
	public SatisRank getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<Object> getHistoricByProcInstId(@RequestBody(required=false) String body){
		
		String sql = dicService.getSqlByBizcode("flow_historics");				
		return super.getHistoricByProcInstId(body, sql);
	}
	
	public String generateBizno(){
		return super.generateBizno();
	}
	
	
	public List<SatisRank> getListByStartDate(String start_date) {
		String sql = "SELECT * FROM %1$s WHERE MODIDATE >= to_date(%2$s, 'YYYY-MM-DD')";
		sql = String.format(sql, this.moduleName, start_date);
		return super.getAllList(sql);
	}
	
	public List<SatisRank> getAllList(String sql){		
		
		return super.getAllList(sql);
	}
}
